﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using WlanDesign;
using Autodesk.AutoCAD.EditorInput;

namespace WlanDesignDll
{
    class SubAttenuationSetCommandHandler : System.Windows.Input.ICommand
    {
        #region ICommand 成员

        public bool CanExecute(object parameter)
        {
            return true;
            //throw new NotImplementedException();
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            MessageBox.Show("请选择对象！");

            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";

            string biuldLyr = ReadIniTxt(iniName, "layer", "building_layer");// 建筑物图层

            Entity acEnt = null;
            bool flag = true;
            while (flag)
            {
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // 以只读方式打开块表记录   Open the Block table record for read
                    BlockTable acBlkTbl;
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                    // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                    BlockTableRecord acBlkTblRec;
                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                    PromptEntityOptions peo = new PromptEntityOptions("\n请选择对象：");
                    PromptEntityResult per = acDoc.Editor.GetEntity(peo);
                    if (per.Status != PromptStatus.OK) flag = false;

                    else
                    {
                        //PromptEntityResult p = acDoc.Editor.GetEntity("请选择对象：");
                        //if (p == null)
                        //{ return; }
                        acEnt = acTrans.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                        // 判断选择的是否为建筑物
                        if (acEnt.Layer == biuldLyr)
                        {
                            flag = false;
                            // 写入ini文件

                            WriteIniTxt(iniName, "handle", "handlevalue", acEnt.Handle.Value.ToString());
                        }
                        else { flag = true; MessageBox.Show("您选择的对象不是建筑物，请重新选择！"); }
                    }                  
                }
            }

            // 判断是否有选中实体
            //Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            //string entId1 = ReadIniTxt(iniName, "handle", "handlevalue");
            if (acEnt != null)
            {
                //long enth = long.Parse(entId1);

                //Handle entId = new Handle(enth);

                bool ret;
                System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out   ret);
                if (ret)
                {
                    System.Windows.Forms.Application.Run(new FormAttenuationSetSub1());
                    //WriteIniTxt(iniName, "handle", "handlevalue","");
                    //   frmMain   为你程序的主窗体，如果是控制台程序不用这句 
                    m.ReleaseMutex();
                }
                else
                {
                }
            }
            else
            {
            }
        }
        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }
        //
        // Summery
        // 从ini文件读取信息
        //
        public static string ReadIniTxt(string sPath, string Section, string Key)
        {
            string ServerName;
            IniFileRW ini = new IniFileRW(sPath);
            ServerName = ini.IniReadValue(Section, Key);
            string strSql = ServerName;
            return strSql;
        }
        #endregion
    }
}
